package com.fanbuchi.app.ins.net;

import android.util.Log;
import android.widget.Toast;

import com.fanbuchi.app.ins.manager.OkHttpManager;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.ResponseBody;
import com.squareup.okhttp.ws.WebSocket;
import com.squareup.okhttp.ws.WebSocketCall;
import com.squareup.okhttp.ws.WebSocketListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.net.ConnectException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import okio.Buffer;
import okio.BufferedSource;

/**
 * Created by win7 on 2016/6/2.
 * @title https://github.com/tguerin/okhttp-websocket-chat
 * @desc https://github.com/dellby/wsmanger
 * android socket
 * https://github.com/koush/AndroidAsync
 */
public  class WebSocketWrapper {
    private static final String TAG="WebSocketAPI";
    private WebSocketWrapper(){}



    public  static void webSocketSendContent(final String content){
        String wsUrl = "ws://120.26.85.230:8080/truck/server";
        Request wsRequest = new Request.Builder()
                .url(wsUrl)
                .build();
        WebSocketCall wsCall = WebSocketCall.create(OkHttpManager.getmOkHttpClient(), wsRequest);

        wsCall.enqueue(new BaseWebSocketListener(){
            @Override
            public void onOpen(WebSocket webSocket, Response response) {
                RequestBody requestBody = RequestBody.create(WebSocket.TEXT, content);
                try {
                    webSocket.sendMessage(requestBody);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onMessage(ResponseBody message) {
            }
        });
        //return wsCall;
    }




    public static String sendHeartParam(){
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("destination", "/heartbeat");
            jsonObject.put("token", "1e0eb14d-e118-4f20-85b6-d8b807331553");
            return jsonObject.toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static String sendSubscribe(){
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("destination", "/order/subscribe");
            jsonObject.put("token", "1e0eb14d-e118-4f20-85b6-d8b807331553");
            return jsonObject.toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

   /* public  class MyWebSocketListener implements WebSocketListener{

        long beginTime=0l;
        @Override
        public void onOpen(com.squareup.okhttp.ws.WebSocket webSocket, Response response) {
            //webSocket.sendPing("");
            //webSocket.sendMessage();
            WebSocketWrapper.webSocket=webSocket;

            beginTime= Calendar.getInstance().getTimeInMillis();
            Log.e(TAG,"onOpen "+response.code());
            if (response.code()==101){
                needRetryConn=false;
                if (taskPool.size()>0){
                    //excute task
                }
            }
        }

        @Override
        public void onFailure(IOException e, Response response) {
            needRetryConn=true;
            if (e instanceof ConnectException){
                //Toast.makeText()
            }
            long cc= Calendar.getInstance().getTimeInMillis()-beginTime;
            if (response!=null){
                Log.e(TAG,"onFailure response="+response);
            }else{
                Log.e(TAG,"onFailure cc=" +(cc/1000) +"s" );
            }
            e.printStackTrace();
        }

        @Override
        public void onMessage(ResponseBody message) {
            try {
                Log.e(TAG,"onMessage message =" +message.string());
                //response = RequestBody.create(WebSocket.TEXT, message.string());
            }catch (IOException e){
                e.printStackTrace();
            }finally {
                try {
                    message.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void onPong(Buffer payload) {
            Log.e(TAG,"onPong payload="+payload);
            try {
                webSocket.close(1004,"client close");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onClose(int code, String reason) {
            //code=1002  reason=invalid close code
            Log.e(TAG,"onClose code="+code +"  reason="+reason);
        }
    }*/




}
